﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using DotNet.Utilities;

namespace Utility
{
    /// <summary>
    /// 时间分段类;
    /// </summary>
    public class TimeBucket
    {
        /// <summary>
        /// 依据服务时间和时间分段大小获得精细的分段时间数据;
        /// </summary>
        /// <param name="timeBuckets">服务时间</param>
        /// <param name="perMultiTime">时间分段大小</param>
        /// <returns>分段时间的List</returns>
        public static List<string> GetAllTimeBucketsList(string timeBuckets, float perMultiTime)
        {
            List<string> timeBucketList = new List<string>();

            string[] parts = StringHelper.StrToArrayByComma(timeBuckets);
            foreach (string part in parts)
            {
                List<string> headAndTail = StringHelper.StrToList(part, '-');
                if (2 == headAndTail.Count)
                {
                    try
                    {
                        float head = float.Parse(headAndTail[0]);
                        float tail = float.Parse(headAndTail[1]);
                        for (float mid = head + perMultiTime; mid <= tail - perMultiTime; )
                        {
                            timeBucketList.Add( head + "-" + mid );
                            head = mid;
                            mid = head + perMultiTime;
                        }
                        timeBucketList.Add( head + "-" + tail ); // 将最后不够划分的归为最后一段;
                    }
                    catch
                    {
                        throw new Exception("时间起止转换失败！");
                    }
                }
                else
                {
                    throw new Exception("时间段中有错误数据！");
                }
            }

            return timeBucketList;
        }

        /// <summary>
        /// 将单个的时间区段转为显示的时间形式;
        /// </summary>      
        public static string ChangeTimeBucketToShowTime(string timeBucket)
        {
            string head = string.Empty;
            string tail = string.Empty;

            List<string> headAndTail = StringHelper.StrToList(timeBucket, '-');
            if (2 == headAndTail.Count)
            {
                head = StringHelper.ChangeDecimalTimeToShowTime(headAndTail[0]);
                tail = StringHelper.ChangeDecimalTimeToShowTime(headAndTail[1]);
            }

            return head + "～" + tail;
        }

        /// <summary>
        /// 依据服务时间和时间分段大小获得分段时间字典;
        /// </summary>       
        public static Dictionary<string, string> GetAllTimeBucketsDictionary(string timeBuckets, float perMultiTime)
        {
            Dictionary<string, string> timeBucketsDictionary = new Dictionary<string, string>();

            List<string> timeBucketList = GetAllTimeBucketsList(timeBuckets, perMultiTime);
            foreach (string timeBucketValue in timeBucketList)
            {
                string timeBucketKey = ChangeTimeBucketToShowTime(timeBucketValue);
                timeBucketsDictionary.Add(timeBucketKey, timeBucketValue);
            }

            return timeBucketsDictionary;
        }
    }
}
